/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.put.dcs_modeler.project.logicalViewExtension.computingModelNodeFactory;


import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.spi.project.ui.support.NodeFactory;
import org.netbeans.spi.project.ui.support.NodeFactorySupport;
import org.netbeans.spi.project.ui.support.NodeList;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.util.Exceptions;
import pl.put.dcs_modeler.exceptions.DCSModelerException;

/**
 *
 * @author LukaszR
 */
public class ComputingModelNodeFactory implements NodeFactory
{
    public static Logger log = Logger.getLogger( ComputingModelNodeFactory.class.getName() );
    private static final ResourceBundle boundle = ResourceBundle.getBundle( "pl/put/dcs_modeler/exceptions/Boundle" );
    Project proj;



    @Override
    public NodeList<?> createNodes( Project project )
    {
        
        this.proj = project;
        String projectDir = project.getProjectDirectory().getPath();
        FileObject projectRsrc = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Project/DCS/DCS Project" );
        String defaultProjectPkg = ( String )projectRsrc.getAttribute( "defaultProjectSrcPackage" );
        ProjectInformation projectInfo = ProjectUtils.getInformation( project );

        // pozyskanie ptrzebnych danych
        String projectName = projectInfo.getName();
        FileObject temp = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/DCS/DCSComputingModel.dcsmodel" );
        String pathToModel = "/src/" + defaultProjectPkg + "/" + projectName + "/" + ( String )temp.getAttribute( "defaultPackage" ) ;
        //If there is no 'nbproject' folder,
        //return an empty list of nodes:
        if ( proj.getProjectDirectory().getFileObject( pathToModel ) == null )
        {
            try
            {
                throw new DCSModelerException( ComputingModelNodeFactory.class.getName(), boundle.getString("Project_is_corrupted._No_models_dir.") );
            }
            catch ( DCSModelerException ex )
            {
                Exceptions.printStackTrace( ex );
            }
            return NodeFactorySupport.fixedNodeList();
        }
        
        try
        {
            ComputingModelNode cmn = new ComputingModelNode( proj, pathToModel );
            cmn.setDisplayName( org.openide.util.NbBundle.getMessage(ComputingModelNodeFactory.class, "ComputingModelNodeName") );
            return NodeFactorySupport.fixedNodeList( cmn );
        }
        catch ( DataObjectNotFoundException ex )
        {
            Exceptions.printStackTrace( ex );
            new DCSModelerException( ComputingModelNodeFactory.class.getName(), ex.getMessage() );
        }
        
        return NodeFactorySupport.fixedNodeList();


    }

    
}
